package com.qingcheng.aspect;

import com.qingcheng.dao.AuditLogMapper;
import com.qingcheng.pojo.log.AuditLog;
import com.qingcheng.util.IdWorker;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ProjectName: qingcheng_parent
 * @ClassName: AuditLogServiceAspect
 * @Time: 2019/6/29
 * @Author: zhou24
 * @Description:
 * @Experience:
 */
@Aspect
@Component
public class AuditLogServiceAspect {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private AuditLogMapper auditLogMapper;
//    @Pointcut("execution(* com.itheima.aop.*.*(..))")
    @Pointcut("@annotation(com.qingcheng.aop.AuditLogAnnotation)")
    public void myAuditLog() {
    }

    // @After("myAuditLog()")
    @AfterReturning("myAuditLog()")
    public void addAuditLog(JoinPoint jp) {
        Signature signature = jp.getSignature();
        String declaringTypeName = signature.getDeclaringTypeName();
        String name = signature.getName();
        Object[] args = jp.getArgs();
        AuditLog auditLog = new AuditLog();
        auditLog.setAuditPerson("登录者");
        auditLog.setAuditTime(new Date());
        auditLog.setId(idWorker.nextId() + "");
        auditLog.setSpuId(args[0].toString());
        auditLog.setMessage(declaringTypeName + "." + name + "。" + args[2].toString());
        auditLogMapper.insert(auditLog);

    }
}
